from abc import ABCMeta, abstractmethod
import random
from time import sleep
from .question import Question
from Tools.console import input, print as p
from Config.file import Restore


class ProblemQuestionWayInterface(metaclass=ABCMeta):

    questions: list

    @abstractmethod
    def __init__(self, question: list):
        pass

    def __iter__(self):
        return self

    @abstractmethod
    def __next__(self) -> Question:
        pass


class RandomQuestion(ProblemQuestionWayInterface):
    questions = []

    def __init__(self, question):
        self.questions = question

    def __next__(self):
        position = random.randint(0, len(self.questions))
        question = self.questions[position]
        return Question(question.get("title"), question.get(
            "right"), question.get("options"))


class RandomQuestionNoRepeat(ProblemQuestionWayInterface):
    questions = []

    def __init__(self, question):
        self.questions = question

    def __next__(self):
        if len(self.questions) == 0:
            raise StopIteration
        position = random.randint(0, len(self.questions) - 1)
        question = self.questions.pop(position)
        return Question(question.get("title"), question.get(
            "right"), question.get("options"))


class SequenceQuestion(ProblemQuestionWayInterface):
    questions = []

    def __init__(self, question):
        self.questions = question

    def __next__(self):
        if len(self.questions) == 0:
            raise StopIteration
        question = self.questions.pop(0)
        return Question(question.get("title"), question.get(
            "right"), question.get("options"))


class CommonQuestion(ProblemQuestionWayInterface):
    questions = []

    def __init__(self, question):
        self.questions = question

    def __next__(self):
        if len(self.questions) == 0:
            raise StopIteration
        position = random.randint(0, len(self.questions) - 1)
        question = self.questions.pop(position)
        return Question(question.get("title"), question.get(
            "right"), question.get("options"))


def store_current_question(store: Restore, data):
    if not store is None:
        p("\r\n是否需要存储: (default no)")
        if store.current_filename != None and store.current_filename.parent != None:
            p("1: 覆盖")
        p("任意按键: 新建")
        i = input("")
        if i == '' or i == None or i == '0':
            return
        if store.current_filename != None and store.current_filename.parent != None and i == '1':
            store.current_filename = store.current_filename.parent
        store.write(data)


class Problem:
    question = []
    way: ProblemQuestionWayInterface = None
    mode = 0
    store: Restore = None

    def __init__(self, question, mode: int, store: Restore):
        ways = [RandomQuestion, RandomQuestionNoRepeat,
                SequenceQuestion, CommonQuestion]
        self.store = store
        if (mode > len(ways) or mode < 1):
            raise "请输入正确的模式"
            return
        self.way = ways[mode - 1](question)
        self.mode = mode - 1
        self.question = question

    def iter(self):
        return self.way

    def run(self):

        for ques in self.iter():
            if self.mode == 3:
                ques.print_ques_with_answer()
                try:
                    input("")
                except KeyboardInterrupt:
                    store_current_question(self.store, self.way.questions)
                    exit(0)
            else:
                ques.print_ques()
                answer = ""
                try:
                    answer = input("答: ")
                except KeyboardInterrupt:
                    store_current_question(self.store, self.way.questions)
                    exit(0)
                if ques.compare_answer(answer.upper()):
                    p("正确", style="green")
                else:
                    p("错误, 正确答案: %s" % ques.right, style="red")
                    self.question.append(ques.to_dict())
            sleep(1)
            print("\n\n")
